const shop = require('../../../mixins/shop');
const app = getApp()
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    tapbar: [{
      name: '全部',
      count: 0
    }, {
      name: '待支付',
      count: 0
    }, {
      name: '待发货',
      count: 0
    }, {
      name: '待收货',
      count: 0
    }, {
      name: '待晒单',
      count: 0
    }],
    baridx: 0,
    particulars: false,
    list: [],
    lidx: 0,
    page: 1,
    loading: false,
    loadmoreShow: false,
    isLastPage: false,
    isEmpty: false,
    oid: 0,
    on: 0,
    evaluate: false,
    goodslist: [],
    refundGoods: [],
    serviceShow: false,
  },
  onLoad: function (options) {
    if(options.type){
      this.setData({
        baridx: options.type
      })
    }
  },
  onShow: function () {
    this.getShopOrderCount();
    this.getShopOrderList();
  },
  changeTabs(e) {
    this.setData({
      baridx: e.detail.activeKey,
      list: [],
      page: 1,
      loading: false,
      loadmoreShow: false,
      isLastPage: false,
      isEmpty: false,
      particulars: false,
    })
    this.getShopOrderList();
  },
  confirmReceipt(e) {
    let that = this;
    let oid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认要确认收货该订单？',
      success(res) {
        if (res.confirm) {
          that.setData({
            oid: oid
          })
          that.getShopConfirmReceipt();
        }
      }
    })
  },
  orderRefund(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let gidx = e.currentTarget.dataset.gidx;
    let on = e.currentTarget.dataset.on;
    wx.showModal({
      title: '提示',
      content: '确定要提交退款申请吗？退款申请操作是不可逆的。',
      confirmText: '提交退款',
      success(res) {
        if (res.confirm) {
          let list = that.data.list;
          let refundGoods = list[index].goods[gidx];
          list[index].goods[gidx].state = 1;
          that.setData({
            refundGoods: refundGoods,
            on: on,
            list: list
          })
          that.getShopOrderRefund();
        }
      }
    })
  },
  refundsFun(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let gidx = e.currentTarget.dataset.gidx;
    let on = e.currentTarget.dataset.on;
    wx.showModal({
      title: '提示',
      content: '确定要提交退货/退款申请吗？退货/退款申请操作是不可逆的。',
      confirmText: '提交申请',
      success(res) {
        if (res.confirm) {
          let list = that.data.list;
          let refundGoods = list[index].goods[gidx];
          list[index].goods[gidx].state = 2;
          that.setData({
            refundGoods: refundGoods,
            on: on,
            list: list
          })
          that.getShopRefundGoods();
        }
      }
    })
  },
  pushDelivery(e) {
    let that = this;
    let on = e.currentTarget.dataset.on;
    that.setData({
      on: on
    })
    that.getShopPushDelivery();
  },
  delOrder(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    let oid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认要删除该订单？删除操作是不可逆的。',
      success(res) {
        if (res.confirm) {
          let list = that.data.list;
          list.splice(idx, 1);
          that.setData({
            oid: oid,
            list: list
          })
          that.getShopDelOrder();
        }
      }
    })
  },
  cancelOrder(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    let oid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认要取消该订单？',
      success(res) {
        if (res.confirm) {
          let list = that.data.list;
          if (that.data.baridx == 0) {
            list[idx].status = 5;
            list[idx].status_text = '已取消';
          } else {
            list.splice(idx, 1);
          }
          that.setData({
            oid: oid,
            list: list
          })
          that.getShopCancelOrder();
        }
      }
    })
  },
  evaluateOrder(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    let goodslist = that.data.list[idx].goods;
    for (let v of goodslist) {
      v.is_check = true;
    }
    that.setData({
      evaluate: true,
      goodslist: goodslist,
    })
  },
  radioGoods(e) {
    let idx = e.currentTarget.dataset.idx;
    let check = e.currentTarget.dataset.check;
    let goodslist = this.data.goodslist;
    if (check) {
      goodslist[idx].is_check = false;
    } else {
      goodslist[idx].is_check = true;
    }
    this.setData({
      goodslist: goodslist,
    })
  },
  ckEvaluate() {
    this.setData({
      evaluate: false,
      goodslist: [],
    })
  },
  toPostsCreat() {
    let that = this;
    let goodslist = that.data.goodslist;
    let postsGoods = [];
    let obj = {};
    goodslist = goodslist.reduce((item, next) => {
      if (next.is_check) {
        obj[next.goods_id] ? '' : obj[next.goods_id] = true && item.push(next);
      }
      return item;
    }, []);
    for (let v of goodslist) {
      let goods = {};
      goods.id = v.goods_id;
      goods.name = v.name;
      goods.pic = v.pic;
      postsGoods.push(goods)
    }
    wx.setStorageSync('postsGoods', postsGoods);
    wx.navigateTo({
      url: '/pages/creat/index/creat',
    })
  },
  toLogistics(e) {
    wx.navigateTo({
      url: '/pages/shop/logistics/logistics?nu=' + e.currentTarget.dataset.nu,
    })
  },
  linfold(e) {
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      lidx: idx,
      particulars: false
    })
  },
  linexpand(e) {
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      lidx: idx,
      particulars: true
    })
  },
  toGoodsDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shop/goods-details/goods-details?id=' + id,
    })
  },
  copyTap(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.no,
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    let that = this;
    that.setData({
      loadmoreShow: true,
      page: that.data.page + 1
    });
    that.getShopOrderList();
  },
  onShareAppMessage: function () {},
}

shop(options)
Page(options)